package com.hertz.appr.dao;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.StatelessSession;
import org.springframework.orm.hibernate3.HibernateTemplate;

//import com.ku6.utcc.util.Constants;

/**
 * 提供dao的所有操作<br>
 * 实现类由spring注入:<br>
 */
public class BaseDaoImpl<T, PK extends Serializable> implements IBaseDao<T, PK> {
	Logger log = Logger.getLogger(BaseDaoImpl.class);
	protected Class<T> entityClass;// DAO所管理的Entity类型.
	private GenericEntityDao<T, PK> gedao;//负责与实体相关的Dao操作 如CRUD
	private GenericDao gdao;//负责与实体无关的操作，如查找，分页
	private SimpleJdbcDao sjdao;

	/**
	 *让spring提供构造函数注入
	 */
	public BaseDaoImpl(Class<T> type) {
		this.entityClass = type;
		log.info("new BaseDaoImpl with T=" + type);
	}

	public BaseDaoImpl() {
	}

	public Class<T> getEntityClass() {
		return entityClass;
	}

	public void setEntityClass(Class<T> entityClass) {
		this.entityClass = entityClass;
	}

	public GenericEntityDao<T, PK> getGedao() {
		return gedao;
	}

	public void setGedao(GenericEntityDao<T, PK> gedao) {
		this.gedao = gedao;
	}

	public GenericDao getGdao() {
		return gdao;
	}

	public void setGdao(GenericDao gdao) {
		this.gdao = gdao;
	}

	public SimpleJdbcDao getSjdao() {
		return sjdao;
	}

	public void setSjdao(SimpleJdbcDao sjdao) {
		this.sjdao = sjdao;
	}


	public void clear() {
		gdao.clear();
	}

	public Query createQuery(String hql, Object... values) {
		return gdao.createQuery(hql, values);
	}

	public void delete(T entityObject) {
		gedao.delete(entityObject);
	}

	public void deleteById(PK id) {
		gedao.deleteById(entityClass,id);
	}

	public void evict(T entityObject) {
		gedao.evict(entityObject);
	}

	public List<T> find(String hql, Object... values) {
		return gdao.find(hql, values);
	}

	public List<T> findByNamedParams(String hql, String[] paramNames,
			Object... values) {
		return gdao.findByNamedParams(hql, paramNames, values);
	}

	public void flush() {
		gdao.flush();
	}

	public List<T> getAll() {
		List<T> list = gedao.getAll(entityClass);
		return list;
	}

	public T getById(PK id) {
		return gedao.getById(entityClass,id);
	}

	public Session getNativeHibernateSession() {
		return gdao.getNativeHibernateSession();
	}

	public StatelessSession getNativeStatelessHibernateSession() {
		return gdao.getNativeStatelessHibernateSession();
	}

	public HibernateTemplate getSpringHibernateTemplate() {
		return gdao.getSpringHibernateTemplate();
	}

	public Iterator<T> iterator(String hql, Object... values) {
		return gdao.iterator(hql, values);
	}

	public SimpleJdbcDao jdbc() {
		return sjdao;
	}

	public T load(PK id) {
		return gedao.load(entityClass,id);
	}

	public void load(T entityObject, PK id) {
		gedao.load(entityObject, id);
	}

	public T merge(T entityObject) {
		return gedao.merge(entityObject);
	}

	public SQLQuery nativeSqlQuery(String sql) {
		return gdao.nativeSqlQuery(sql);
	}

	public DaoPage<T> pagedQuery(String countHql, String hql, Long pageNo,
			Integer pageSize, Object... values) {
		return gdao.pagedQuery(countHql, hql, pageNo, pageSize, values);
	}

	public DaoPage<T> pagedQueryByStartNo(String countHql, String hql,
			int startNo, int pageSize, Object... values) {
		return gdao.pagedQueryByStartNo(countHql, hql, startNo, pageSize,
				values);
	}

	public void refresh(T entityObject) {
		gedao.refresh(entityObject);
	}

	public void save(T entityObject) {
		gedao.save(entityObject);
	}

	public void update(T entityObject){
		gedao.update(entityObject);
	}

	public Integer execute(String hql, Object... values) {
		// TODO Auto-generated method stub
		return gdao.execute(hql, values);
	}

	public List queryList(String hql,int firstResult,int maxResults,Object... values){
		return gdao.queryList(hql,firstResult,maxResults, values);
	}


}